package org.ly.uap.client.jaas;

import java.io.IOException;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;

public class ServiceAndTicketCallbackHandler
        implements CallbackHandler {
    private final String service;
    private final String ticket;

    public ServiceAndTicketCallbackHandler(String service, String ticket) {
        this.service = service;
        this.ticket = ticket;
    }

    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
        for (Callback callback : callbacks)
            if ((callback instanceof NameCallback))
                ((NameCallback) callback).setName(this.service);
            else if ((callback instanceof PasswordCallback))
                ((PasswordCallback) callback).setPassword(this.ticket.toCharArray());
            else
                throw new UnsupportedCallbackException(callback, "Callback not supported.");
    }
}
